using System;
using System.Drawing;
using System.Windows.Forms;

namespace TShooter.TfsWord.Win.WizardSupport
{
    /// <summary>
    /// This control works exactly the same as a normal
    /// panel but, if the panel is docked at the bottom of the form,
    /// then a sizing grip is added and a horizontal 3D line
    /// is added at the top of panel.
    /// </summary>
    public class GripPanel : Panel
    {
        private const int HTBOTTOMRIGHT = 17;
        private const int WM_NCHITTEST = 0x84;
        private const int WM_SIZE = 0x05;
        private SizeGripStyle _SizeGripStyle = SizeGripStyle.Auto;

        public SizeGripStyle SizeGripStyle
        {
            get { return _SizeGripStyle; }
            set 
            { 
                _SizeGripStyle = value; 
                Invalidate();
            }
        }

        /// <summary>
        /// Catch some windows messages
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            // Only catch messages if the panel is docked to the bottom
            if (Dock == DockStyle.Bottom)
            {
                switch (m.Msg)
                {
                        // If the panel is being resized then we
                        // need to redraw its contents
                    case WM_SIZE:
                        Invalidate();
                        break;

                        // If the system is asking where the mouse pointer is,
                        // we need to check whether it is over our sizing grip
                    case WM_NCHITTEST:

                        // Convert to client co-ordinates of parent
                        Form form = FindForm();
                        if (form != null)
                        {
                            Point p = form.PointToClient(new Point((int) m.LParam));
                            int x = p.X;
                            int y = p.Y;
                            Rectangle rect = Bounds;

                            // Is the mouse pointer over our sizing group?
                            // (Use 12 pixels rather than 16 otherwise
                            // too large an area is checked)
                            if (x >= rect.X + rect.Width - 12 &&
                                x <= rect.X + rect.Width &&
                                y >= rect.Y + rect.Height - 12 &&
                                y <= rect.Y + rect.Height)
                            {
                                // Yes, so tell windows it is in the lower-right
                                // corner of a border of a resizable window
                                // Windows will then do the neccessary resizing
                                m.Result = new IntPtr(HTBOTTOMRIGHT);
                                return;
                            }
                        }
                        break;
                }
            }

            // Do the normal message handling
            base.WndProc(ref m);
        }

        /// <summary>
        /// Override to add the border line at the top
        /// of the panel and the size grip itself
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            // Do the normal painting
            base.OnPaint(e);

            bool showGrip =
                (_SizeGripStyle == SizeGripStyle.Auto && (Dock == DockStyle.Bottom)) ||
                (_SizeGripStyle == SizeGripStyle.Show);

            if (showGrip)
            {
                //ControlPaint.DrawBorder3D(e.Graphics, ClientRectangle,
                //                          Border3DStyle.Raised, Border3DSide.Top);
                ControlPaint.DrawSizeGrip(e.Graphics, BackColor,
                                          Width - 16, Height - 16, 16, 16);
            }
        }
    }
}